#include "widget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QStringList>

#include "enter_leave_widget.h"
#include "press_move_release_widget.h"
#include "keywidget.h"
#include "timer_widget.h"
#include "drag_widget.h"
#include "PainterWidget.h"
#include "context_widget.h"
#include "propagate_widget.h"

Widget::Widget(QWidget *parent)
	: QWidget(parent)
{
	this->setWindowTitle("Qt事件演示");
	/* 整体采用水平布局 */
	QHBoxLayout* mainLayout = new QHBoxLayout();
	mainLayout->setSpacing(0);
	mainLayout->setContentsMargins(0, 0, 0, 0);

	// > 导航窗体
	navWidget = new QWidget();
	QVBoxLayout * leftLayout = new QVBoxLayout();
	leftLayout->setSpacing(0);
	leftLayout->setContentsMargins(0, 0, 0, 0);
	navWidget->setLayout(leftLayout);
	InitNav();

	// > 主窗体
	mainWidget = new QWidget();
	mainWidget->setMinimumWidth(400);
	QVBoxLayout* rightLayout = new QVBoxLayout();
	rightLayout->setSpacing(0);
	rightLayout->setContentsMargins(0, 0, 0, 0);
	stackedWidget = new QStackedWidget();
	initMain();

	rightLayout->addWidget(stackedWidget);
	mainWidget->setLayout(rightLayout);

	mainLayout->addWidget(navWidget);
	mainLayout->addWidget(mainWidget);

	this->setLayout(mainLayout);
}


Widget::~Widget() {}

void Widget::InitNav()
{
	// 按钮文字集合
	QStringList names;
	names << "鼠标进入/离开"
		  << "鼠标按下/移动/释放"
		  << "键盘事件"
		  << "定时器事件"
		  << "拖动事件"
		  << "绘图事件"
		  << "右键菜单"
		  << "总结：事件的传递流程";

	buttonGroup = new QButtonGroup();
	//自动生成按钮
	for (int i = 0; i < names.count(); i++ ) {
		QPushButton* btn = new QPushButton();
		//设置按钮固定高度
		btn->setMinimumHeight(60);
		//设置按钮的文字
		btn->setText(QString("%1. %2").arg(i + 1, 2, 10, QChar('0')).arg(names.at(i)));
		//设置按钮可选中按下类似复选框的功能
		btn->setCheckable(true);
		// 将按钮添加到 btnGroup
		buttonGroup->addButton(btn, i);
		//将按钮加入到布局
		navWidget->layout()->addWidget(btn);
		// 添加信号槽
		connect(btn,&QPushButton::clicked,this,&Widget::button_clicked);
	}
	// 设置按钮的样式
	navWidget->setStyleSheet(R"(
			QPushButton {
					font: 25px;
					text-align : left;
			}
	)");
}

void Widget::initMain()
{
	stackedWidget->addWidget(new EnterLeaveWidget());
	stackedWidget->addWidget(new PressMoveReleaseWidget());
	stackedWidget->addWidget(new KeyWidget());
	stackedWidget->addWidget(new TimerWidget());
	stackedWidget->addWidget(new DragWidget());
	stackedWidget->addWidget(new PainterWidget());
	stackedWidget->addWidget(new ContextWidget());
	stackedWidget->addWidget(new PropagateWidget());
}

void Widget::button_clicked()
{
	int id=buttonGroup->checkedId();
	stackedWidget->setCurrentIndex(id);
}
